﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataLayer;
using System.Xml;

namespace DataLayer
{
    public class ResourcesHelper:SystemFileHelper
    {
        static ResourcesHelper instance;
        static XmlDocument xmlDocument=null;
        public static string DestinationFile;
        static public ResourcesHelper GetInstance()
        {
            if (instance == null)
                instance = new ResourcesHelper();
            return instance;
        }
        
        static public void CreateNewResourceFile(string folderDir)
        {
            xmlDocument = new XmlDocument();
            XmlNode rsList=xmlDocument.CreateNode(XmlNodeType.Element, "ResourceList", "");
            xmlDocument.AppendChild(rsList);
            XmlNode maxID = xmlDocument.CreateNode(XmlNodeType.Element, "MaxID", "");
            maxID.InnerText = "0";
            rsList.AppendChild(maxID);
            DestinationFile = folderDir + "\\" + "Resources.xml";
            xmlDocument.Save(DestinationFile);
        }
        private ResourcesHelper()
        {
            this.listObject = LoadAll();
        }

        static public int MaxID;
        public override List<SystemFile> LoadAll()
        {

            if (listObject == null)
                listObject = new List<SystemFile>();

            xmlDocument = new XmlDocument();
            XmlTextReader reader = new XmlTextReader(DestinationFile);
            reader.Read();
            // load reader
            xmlDocument.Load(reader);
            XmlNode MaxIDNode= xmlDocument.FirstChild.SelectSingleNode("./MaxID");
            MaxID = int.Parse(MaxIDNode.InnerText);
            for (int i = 0; i < xmlDocument.ChildNodes[0].ChildNodes.Count; i++)
            {
                XmlNode currentNode = xmlDocument.ChildNodes[0].ChildNodes[i];
                if (currentNode.Name == "Resource")
                {
                    Resource rs = new Resource();
                    rs.Path = currentNode.SelectSingleNode("Path").InnerText;
                    rs.Id = int.Parse(currentNode.SelectSingleNode("ID").InnerText);
                
                //Lack of Resource Loader.

                listObject.Add(rs);
                }
                else
                {
                    MaxID = int.Parse(currentNode.InnerText);
                }

            }
            reader.Close();
            return listObject;
        }


        public string IDToPath(int id)
        {
            for (int i = 0; i < listObject.Count; i++)
            {
                Resource rs = listObject[i] as Resource;
                if (rs.Id == id)
                {
                    return rs.Path;
                }
            }
            return null;
        }

        public int PathToID(string path)
        {
            for (int i = 0; i < listObject.Count; i++)
            {
                Resource rs = listObject[i] as Resource;
                if (rs.Path == path)
                {
                    return rs.Id;
                }
            }
            return-1;
        }
        public int AddResource(string path)
        {
            Resource rs = new Resource();
            rs.Path = path;
            rs.Id = MaxID + 1;
            MaxID += 1;
            XmlNode resNode=xmlDocument.CreateNode(XmlNodeType.Element, "Resource", "");
            XmlNode idNode = xmlDocument.CreateNode(XmlNodeType.Element, "ID", "");
            idNode.InnerText = rs.Id.ToString();
            XmlNode pathNode = xmlDocument.CreateNode(XmlNodeType.Element, "Path", "");
            pathNode.InnerText = path;
            resNode.AppendChild(idNode);
            resNode.AppendChild(pathNode);
            xmlDocument.FirstChild.AppendChild(resNode);
            XmlNode maxIDNode = xmlDocument.FirstChild.SelectSingleNode("./MaxID");
            maxIDNode.InnerText = MaxID.ToString();
            this.listObject.Add(rs);
            Save();
            return MaxID;
        }
     
        static public void DeleteResource(string path)
        {
            XmlNode oldNode=xmlDocument.FirstChild.SelectSingleNode("./Resource/Path[.='"+path+"']");
            if (oldNode != null)
            {
                xmlDocument.FirstChild.RemoveChild(oldNode.ParentNode);
                Save();
            }
        }
        static public void Save()
        {
            xmlDocument.Save(DestinationFile);
        }
        override public void Update()
         {
             if (listObject != null)
             {
                 XmlDocument doc = new XmlDocument();
                 XmlElement rootnode = doc.CreateElement("ResourceList");
                 doc.AppendChild(rootnode);
                 XmlElement Maxnode = doc.CreateElement("MaxID");
                 int count = listObject.Count;
                 if (count > 0)
                 {
                     Maxnode.InnerText = (listObject[count - 1] as Resource).Id.ToString();
                     rootnode.AppendChild(Maxnode);
                     for (int i = 0; i < listObject.Count; i++)
                     {
                         listObject[i].AddToXmlDoc(doc, rootnode);
                     }
                     doc.Save(DestinationFile);
                 }
                 
             }
         }
    }
}
